//提交框为空验证
$('.load').on('click', function () {
    let usernameText = checkUsername()
    let passwordText = checkPassword()
    let username = $('.username').val()
    let password = $('.password').val()
    console.log(checkAll(username, password));
    if (usernameText && passwordText) {
        if (checkAll(username, password)) {
            let userNaw = {}
            userNaw.username = $('.username').val()
            userNaw.password = $('.password').val()
            localStorage.setItem('userNaw', JSON.stringify(userNaw))
            location.href = `http://127.0.0.1:5555/%E7%86%8A%E7%8C%AB%E4%BC%98%E9%80%89/src/pages/index.html`
        } else {
            alert('密码错误')
        }
    }
})

function checkAll(username, password) {
    let userArr = JSON.parse(localStorage.getItem('user'))
    for (let i = 0; i < userArr.length; i++) {
        if (userArr[i].username == username) {
            if (userArr[i].password == password) {
                return true
            } else {
                return false
            }
        }
    }
}

function testl() {
    let userArr = JSON.parse(localStorage.getItem('user')) || []
    let users={}
    users.username = $('.username2').val()
    users.password = $('.password2').val()
    userArr.push(users)
    localStorage.setItem('user', JSON.stringify(userArr))
}

//失去焦点验证
$('.username').on('blur', function () {
    checkUsername()
})
$('.password').on('blur', function () {
    checkPassword()
})
//判定用户名是否为空
function checkUsername() {
    let username = $('.username').val()
    if (username == '') {
        $('.user').html('用户名不能为空')
        return false
    } else {
        $('.user').html('')
        return true
    }
}
//判定密码是否为空
function checkPassword() {
    let password = $('.password').val()
    if (password == '') {
        $('.psw').html('密码不能为空')
        return false
    } else {
        $('.psw').html('')
        return true
    }
}
//注册按钮弹出注册选项
$('.register').on('click', function () {
    $('.registerList').css('display', 'block')
    //失去焦点验证
    $('.username2').on('blur', function () {
        checkUsername2()
    })
    $('.password2').on('blur', function () {
        checkPassword2()
        testPassword()
    })
    $('.email').on('blur', function () {
        checkEmail()
        testEmail()
    })
    $('.register2').on('click', function () {
        if (testPassword() & checkUsername2() & checkPassword2() & checkEmail() & testEmail()) {
            testl()
            $('.user2').html('')
            $('.psw2').html('')
            $('.email').html('')
            $('.registerList').css('display', 'none')
        } else {
            alert('格式有误')
        }

    })
})
// register()
//判定用户名是否为空
function checkUsername2() {
    let username2 = $('.username2').val()
    if (username2 == '') {
        $('.user2').html('用户名不能为空')
        return false
    } else {
        $('.user2').html('')
        return true
    }
}
//判定密码是否为空
function checkPassword2() {
    let password2 = $('.password2').val()
    if (password2 == '') {
        $('.psw2').html('密码不能为空')
        return false
    } else {
        $('.psw2').html('')
        return true
    }
}

function checkEmail() {
    let email = $('.email').val()
    if (email == '') {
        $('.email').html('邮箱地址不能为空')
        return false
    } else {
        $('.email').html('')
        return true
    }
}
//验证邮箱账号   
function testEmail() {
    let email = $('.email').val()
    let reg = /[0-9a-zA-Z]+@[0-9a-zA-Z]+\.cn|\.com$/
    if (!reg.test(email)) {
        $('.email').html('邮箱地址不能为空')
        return false
    } else {
        $('.email').html('')
        return true
    }
}
//判断密码强度
function testPassword() {
    let passwordReg = /^[A-Z][0-9a-zA-Z]{7,}/
    let password = $('.password2').val()
    if (!passwordReg.test(password)) {
        $('.psw2').html('密码格式错误')
        return false
    } else {
        $('.psw2').html('')
        return true
    }
}